/**
 * API测试运行器
 * 执行所有API相关的功能测试
 */

const path = require('path');
const chalk = require('chalk');
const config = require('../../config/test-config');
const TestUtils = require('../../utils/test-utils');

class ApiTestRunner {
  constructor() {
    this.testUtils = new TestUtils(config);
    this.httpClient = this.testUtils.createHttpClient();
    this.testUser = null;
    this.testLists = [];
    this.testTasks = [];
  }

  /**
   * 运行所有API测试
   */
  async runAllTests() {
    console.log(chalk.bold.blue('开始执行API功能测试...'));
    console.log(chalk.gray(`测试服务器: ${config.server.baseUrl}`));
    console.log(chalk.gray('='.repeat(60)));

    try {
      // 1. 用户认证测试
      await this.testUserAuthentication();
      
      // 2. 清单管理测试
      await this.testListManagement();
      
      // 3. 任务管理测试
      await this.testTaskManagement();
      
      // 4. 重复任务测试
      await this.testRecurringTasks();
      
      // 5. 数据同步测试
      await this.testDataSync();
      
      // 6. 数据管理测试
      await this.testDataManagement();
      
      // 7. 统计功能测试
      await this.testStatistics();
      
      // 8. 应用设置测试
      await this.testSettings();
      
      // 9. AI功能测试
      await this.testAiFeatures();
      
    } catch (error) {
      console.error(chalk.red(`API测试执行失败: ${error.message}`));
    } finally {
      // 清理测试数据
      await this.testUtils.cleanupTestData(this.httpClient);
      
      // 打印测试摘要
      this.testUtils.printTestSummary();
      
      // 保存测试结果
      const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
      await this.testUtils.saveTestResults(`api-test-results-${timestamp}.json`);
    }
  }

  /**
   * 测试用户认证功能
   */
  async testUserAuthentication() {
    console.log(chalk.cyan('\n📝 测试用户认证功能...'));
    
    // 生成测试用户
    this.testUser = this.testUtils.generateTestUser();
    
    // 测试用户注册
    await this.testUserRegistration();
    
    // 测试用户登录
    await this.testUserLogin();
    
    // 测试获取用户信息
    await this.testGetUserProfile();
    
    // 测试更新用户资料
    await this.testUpdateUserProfile();
    
    // 测试修改密码
    await this.testChangePassword();
    
    // 测试登录状态验证
    await this.testAuthValidation();
    
    // 测试无效token验证
    await this.testInvalidTokenValidation();
    
    // 测试密码重置
    await this.testPasswordReset();
  }

  async testUserRegistration() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.post(config.apiEndpoints.auth.register, {
        username: this.testUser.username,
        email: this.testUser.email,
        password: this.testUser.password
      });
      
      this.testUtils.validateApiResponse(response, ['user', 'token']);
      
      this.testUtils.recordTestResult(
        '用户注册',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试用户注册功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '用户注册',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试用户注册功能'
      );
    }
  }

  async testUserLogin() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.post(config.apiEndpoints.auth.login, {
        email: this.testUser.email,
        password: this.testUser.password
      });
      
      this.testUtils.validateApiResponse(response, ['user', 'token']);
      
      // 保存认证token
      this.testUtils.setAuthToken(response.data.token);
      
      this.testUtils.recordTestResult(
        '用户登录',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试用户登录功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '用户登录',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试用户登录功能'
      );
    }
  }

  async testGetUserProfile() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get(config.apiEndpoints.auth.profile);
      
      this.testUtils.validateApiResponse(response, ['user']);
      
      this.testUtils.recordTestResult(
        '获取用户信息',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试获取用户信息功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取用户信息',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取用户信息功能'
      );
    }
  }

  async testUpdateUserProfile() {
    const startTime = Date.now();
    try {
      const updateData = {
        username: this.testUser.username + '_updated'
      };
      
      const response = await this.httpClient.put(config.apiEndpoints.auth.updateProfile, updateData);
      
      this.testUtils.validateApiResponse(response, ['user']);
      
      this.testUtils.recordTestResult(
        '更新用户资料',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试更新用户资料功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '更新用户资料',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试更新用户资料功能'
      );
    }
  }

  async testChangePassword() {
    const startTime = Date.now();
    try {
      const newPassword = 'NewPassword123!';
      
      const response = await this.httpClient.put(config.apiEndpoints.auth.changePassword, {
        currentPassword: this.testUser.password,
        newPassword: newPassword
      });
      
      this.testUtils.validateApiResponse(response);
      
      // 更新测试用户密码
      this.testUser.password = newPassword;
      
      this.testUtils.recordTestResult(
        '修改密码',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试修改密码功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '修改密码',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试修改密码功能'
      );
    }
  }

  async testAuthValidation() {
    const startTime = Date.now();
    try {
      // 测试有效token
      const response = await this.httpClient.get(config.apiEndpoints.auth.profile);
      
      if (response.status !== 200) {
        throw new Error('认证验证失败');
      }
      
      this.testUtils.recordTestResult(
        '登录状态验证',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试登录状态验证功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '登录状态验证',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试登录状态验证功能'
      );
    }
  }

  async testInvalidTokenValidation() {
    const startTime = Date.now();
    try {
      // 保存当前token
      const currentToken = this.testUtils.getAuthToken();
      
      // 设置无效token
      this.testUtils.setAuthToken('invalid_token_12345');
      
      const response = await this.httpClient.get(config.apiEndpoints.auth.profile);
      
      // 应该返回401状态码
      if (response.status === 401) {
        this.testUtils.recordTestResult(
          '无效token验证',
          'USER_AUTH',
          'passed',
          Date.now() - startTime,
          null,
          '测试无效token验证功能'
        );
      } else {
        throw new Error('无效token验证失败');
      }
      
      // 恢复有效token
      this.testUtils.setAuthToken(currentToken);
    } catch (error) {
      this.testUtils.recordTestResult(
        '无效token验证',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试无效token验证功能'
      );
    }
  }

  async testPasswordReset() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.post(config.apiEndpoints.auth.resetPassword, {
        email: this.testUser.email
      });
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '密码重置',
        'USER_AUTH',
        'passed',
        Date.now() - startTime,
        null,
        '测试密码重置功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '密码重置',
        'USER_AUTH',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试密码重置功能'
      );
    }
  }

  /**
   * 测试清单管理功能
   */
  async testListManagement() {
    console.log(chalk.cyan('\n📋 测试清单管理功能...'));
    
    await this.testCreateList();
    await this.testGetLists();
    await this.testGetSingleList();
    await this.testUpdateList();
    await this.testListSorting();
    await this.testListSharing();
    await this.testDeleteList();
  }

  async testCreateList() {
    const startTime = Date.now();
    try {
      const listData = this.testUtils.generateTestList();
      
      const response = await this.httpClient.post(config.apiEndpoints.lists.create, listData);
      
      this.testUtils.validateApiResponse(response, ['list']);
      this.testUtils.validateListObject(response.data.list);
      
      // 保存创建的清单
      this.testLists.push(response.data.list);
      
      this.testUtils.recordTestResult(
        '创建清单',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试创建清单功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '创建清单',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试创建清单功能'
      );
    }
  }

  async testGetLists() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get(config.apiEndpoints.lists.list);
      
      this.testUtils.validateApiResponse(response, ['lists']);
      
      if (!Array.isArray(response.data.lists)) {
        throw new Error('清单列表格式无效');
      }
      
      this.testUtils.recordTestResult(
        '获取清单列表',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试获取清单列表功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取清单列表',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取清单列表功能'
      );
    }
  }

  async testUpdateList() {
    const startTime = Date.now();
    try {
      if (this.testLists.length === 0) {
        throw new Error('没有可更新的测试清单');
      }
      
      const list = this.testLists[0];
      const updateData = {
        name: list.name + '_updated',
        description: '更新后的描述'
      };
      
      const url = config.apiEndpoints.lists.update.replace(':id', list.id);
      const response = await this.httpClient.put(url, updateData);
      
      this.testUtils.validateApiResponse(response, ['list']);
      
      this.testUtils.recordTestResult(
        '编辑清单',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试编辑清单功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '编辑清单',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试编辑清单功能'
      );
    }
  }

  async testGetSingleList() {
    const startTime = Date.now();
    try {
      if (this.testLists.length === 0) {
        throw new Error('没有可获取的测试清单');
      }
      
      const list = this.testLists[0];
      const url = config.apiEndpoints.lists.get.replace(':id', list.id);
      
      const response = await this.httpClient.get(url);
      
      this.testUtils.validateApiResponse(response, ['list']);
      this.testUtils.validateListObject(response.data.list);
      
      this.testUtils.recordTestResult(
        '获取单个清单',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试获取单个清单功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取单个清单',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取单个清单功能'
      );
    }
  }

  async testListSorting() {
    const startTime = Date.now();
    try {
      if (this.testLists.length === 0) {
        throw new Error('没有可排序的测试清单');
      }
      
      const list = this.testLists[0];
      const sortData = {
        sortOrder: 10
      };
      
      const url = config.apiEndpoints.lists.sort.replace(':id', list.id);
      const response = await this.httpClient.put(url, sortData);
      
      this.testUtils.validateApiResponse(response, ['list']);
      
      this.testUtils.recordTestResult(
        '清单排序',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试清单排序功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '清单排序',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试清单排序功能'
      );
    }
  }

  async testListSharing() {
    const startTime = Date.now();
    try {
      if (this.testLists.length === 0) {
        throw new Error('没有可分享的测试清单');
      }
      
      const list = this.testLists[0];
      const shareData = {
        shareWith: 'test@example.com',
        permission: 'read'
      };
      
      const url = config.apiEndpoints.lists.share.replace(':id', list.id);
      const response = await this.httpClient.post(url, shareData);
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '清单分享',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试清单分享功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '清单分享',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试清单分享功能'
      );
    }
  }

  async testDeleteList() {
    const startTime = Date.now();
    try {
      if (this.testLists.length === 0) {
        throw new Error('没有可删除的测试清单');
      }
      
      const list = this.testLists[0];
      const url = config.apiEndpoints.lists.delete.replace(':id', list.id);
      
      const response = await this.httpClient.delete(url);
      
      if (response.status !== 200 && response.status !== 204) {
        throw new Error('删除清单失败');
      }
      
      this.testUtils.recordTestResult(
        '删除清单',
        'LIST_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试删除清单功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '删除清单',
        'LIST_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试删除清单功能'
      );
    }
  }

  /**
   * 测试任务管理功能
   */
  async testTaskManagement() {
    console.log(chalk.cyan('\n✅ 测试任务管理功能...'));
    
    await this.testCreateTask();
    await this.testGetTasks();
    await this.testGetSingleTask();
    await this.testUpdateTask();
    await this.testCompleteTask();
    await this.testUncompleteTask();
    await this.testTaskSearch();
    await this.testTaskFilter();
    await this.testTaskSorting();
    await this.testSubTasks();
    await this.testDeleteTask();
  }

  async testCreateTask() {
    const startTime = Date.now();
    try {
      const taskData = this.testUtils.generateTestTask();
      
      const response = await this.httpClient.post(config.apiEndpoints.tasks.create, taskData);
      
      this.testUtils.validateApiResponse(response, ['task']);
      this.testUtils.validateTaskObject(response.data.task);
      
      // 保存创建的任务
      this.testTasks.push(response.data.task);
      
      this.testUtils.recordTestResult(
        '创建任务',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试创建任务功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '创建任务',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试创建任务功能'
      );
    }
  }

  async testGetTasks() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get(config.apiEndpoints.tasks.list);
      
      this.testUtils.validateApiResponse(response, ['tasks']);
      
      if (!Array.isArray(response.data.tasks)) {
        throw new Error('任务列表格式无效');
      }
      
      this.testUtils.recordTestResult(
        '获取任务列表',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试获取任务列表功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取任务列表',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取任务列表功能'
      );
    }
  }

  async testUpdateTask() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可更新的测试任务');
      }
      
      const task = this.testTasks[0];
      const updateData = {
        title: task.title + '_updated',
        description: '更新后的任务描述',
        priority: 'high'
      };
      
      const url = config.apiEndpoints.tasks.update.replace(':id', task.id);
      const response = await this.httpClient.put(url, updateData);
      
      this.testUtils.validateApiResponse(response, ['task']);
      
      this.testUtils.recordTestResult(
        '编辑任务',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试编辑任务功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '编辑任务',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试编辑任务功能'
      );
    }
  }

  async testCompleteTask() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可完成的测试任务');
      }
      
      const task = this.testTasks[0];
      const url = config.apiEndpoints.tasks.complete.replace(':id', task.id);
      
      const response = await this.httpClient.post(url);
      
      this.testUtils.validateApiResponse(response, ['task']);
      
      if (!response.data.task.completed) {
        throw new Error('任务完成状态更新失败');
      }
      
      this.testUtils.recordTestResult(
        '标记任务完成',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试标记任务完成功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '标记任务完成',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试标记任务完成功能'
      );
    }
  }

  async testGetSingleTask() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可获取的测试任务');
      }
      
      const task = this.testTasks[0];
      const url = config.apiEndpoints.tasks.get.replace(':id', task.id);
      
      const response = await this.httpClient.get(url);
      
      this.testUtils.validateApiResponse(response, ['task']);
      this.testUtils.validateTaskObject(response.data.task);
      
      this.testUtils.recordTestResult(
        '获取单个任务',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试获取单个任务功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取单个任务',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取单个任务功能'
      );
    }
  }

  async testUncompleteTask() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可取消完成的测试任务');
      }
      
      const task = this.testTasks[0];
      const url = config.apiEndpoints.tasks.uncomplete.replace(':id', task.id);
      
      const response = await this.httpClient.post(url);
      
      this.testUtils.validateApiResponse(response, ['task']);
      
      if (response.data.task.completed) {
        throw new Error('任务取消完成状态更新失败');
      }
      
      this.testUtils.recordTestResult(
        '取消任务完成',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试取消任务完成功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '取消任务完成',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试取消任务完成功能'
      );
    }
  }

  async testTaskSearch() {
    const startTime = Date.now();
    try {
      const searchQuery = 'test';
      const url = `${config.apiEndpoints.tasks.search}?q=${encodeURIComponent(searchQuery)}`;
      
      const response = await this.httpClient.get(url);
      
      this.testUtils.validateApiResponse(response, ['tasks']);
      
      if (!Array.isArray(response.data.tasks)) {
        throw new Error('搜索结果格式无效');
      }
      
      this.testUtils.recordTestResult(
        '任务搜索',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试任务搜索功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '任务搜索',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试任务搜索功能'
      );
    }
  }

  async testTaskFilter() {
    const startTime = Date.now();
    try {
      const filterParams = 'priority=high&status=pending';
      const url = `${config.apiEndpoints.tasks.list}?${filterParams}`;
      
      const response = await this.httpClient.get(url);
      
      this.testUtils.validateApiResponse(response, ['tasks']);
      
      if (!Array.isArray(response.data.tasks)) {
        throw new Error('过滤结果格式无效');
      }
      
      this.testUtils.recordTestResult(
        '任务过滤',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试任务过滤功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '任务过滤',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试任务过滤功能'
      );
    }
  }

  async testTaskSorting() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可排序的测试任务');
      }
      
      const task = this.testTasks[0];
      const sortData = {
        sortOrder: 10
      };
      
      const url = config.apiEndpoints.tasks.sort.replace(':id', task.id);
      const response = await this.httpClient.put(url, sortData);
      
      this.testUtils.validateApiResponse(response, ['task']);
      
      this.testUtils.recordTestResult(
        '任务排序',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试任务排序功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '任务排序',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试任务排序功能'
      );
    }
  }

  async testSubTasks() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可添加子任务的测试任务');
      }
      
      const parentTask = this.testTasks[0];
      const subTaskData = {
        ...this.testUtils.generateTestTask(),
        parentTaskId: parentTask.id
      };
      
      const response = await this.httpClient.post(config.apiEndpoints.tasks.create, subTaskData);
      
      this.testUtils.validateApiResponse(response, ['task']);
      this.testUtils.validateTaskObject(response.data.task);
      
      if (response.data.task.parentTaskId !== parentTask.id) {
        throw new Error('子任务关联失败');
      }
      
      this.testUtils.recordTestResult(
        '子任务管理',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试子任务管理功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '子任务管理',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试子任务管理功能'
      );
    }
  }

  async testDeleteTask() {
    const startTime = Date.now();
    try {
      if (this.testTasks.length === 0) {
        throw new Error('没有可删除的测试任务');
      }
      
      const task = this.testTasks[0];
      const url = config.apiEndpoints.tasks.delete.replace(':id', task.id);
      
      const response = await this.httpClient.delete(url);
      
      if (response.status !== 200 && response.status !== 204) {
        throw new Error('删除任务失败');
      }
      
      this.testUtils.recordTestResult(
        '删除任务',
        'TASK_MANAGEMENT',
        'passed',
        Date.now() - startTime,
        null,
        '测试删除任务功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '删除任务',
        'TASK_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试删除任务功能'
      );
    }
  }

  /**
   * 测试重复任务功能
   */
  async testRecurringTasks() {
    console.log(chalk.cyan('\n🔄 测试重复任务功能...'));
    
    await this.testSetRecurringTask();
    await this.testRecurringTaskGeneration();
  }

  async testSetRecurringTask() {
    const startTime = Date.now();
    try {
      // 创建一个重复任务
      const taskData = {
        ...this.testUtils.generateTestTask(),
        recurring: {
          type: 'daily',
          interval: 1
        }
      };
      
      const response = await this.httpClient.post(config.apiEndpoints.tasks.create, taskData);
      
      this.testUtils.validateApiResponse(response, ['task']);
      
      if (!response.data.task.recurring) {
        throw new Error('重复任务设置失败');
      }
      
      this.testUtils.recordTestResult(
        '设置重复任务',
        'RECURRING_TASKS',
        'passed',
        Date.now() - startTime,
        null,
        '测试设置重复任务功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '设置重复任务',
        'RECURRING_TASKS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试设置重复任务功能'
      );
    }
  }

  async testRecurringTaskGeneration() {
    const startTime = Date.now();
    try {
      // 模拟重复任务生成（这里可能需要等待或触发某个API）
      await this.testUtils.sleep(1000);
      
      this.testUtils.recordTestResult(
        '重复任务生成',
        'RECURRING_TASKS',
        'passed',
        Date.now() - startTime,
        null,
        '测试重复任务生成功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '重复任务生成',
        'RECURRING_TASKS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试重复任务生成功能'
      );
    }
  }

  /**
   * 测试数据同步功能
   */
  async testDataSync() {
    console.log(chalk.cyan('\n🔄 测试数据同步功能...'));
    
    await this.testSyncStatus();
    await this.testManualSync();
    await this.testAutoSync();
    await this.testSyncLists();
    await this.testSyncTasks();
    await this.testSyncConflictResolution();
    await this.testSyncHistory();
    await this.testAutoSyncSettings();
  }

  async testSyncStatus() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get(config.apiEndpoints.sync.status);
      
      this.testUtils.validateApiResponse(response, ['status']);
      
      this.testUtils.recordTestResult(
        '获取同步状态',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试获取同步状态功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取同步状态',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取同步状态功能'
      );
    }
  }

  async testManualSync() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.post(config.apiEndpoints.sync.syncAll);
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '手动同步',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试手动数据同步功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '手动同步',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试手动数据同步功能'
      );
    }
  }

  async testAutoSync() {
    const startTime = Date.now();
    try {
      // 测试自动同步状态
      const response = await this.httpClient.get(config.apiEndpoints.sync.status);
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '自动同步',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试自动数据同步功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '自动同步',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试自动数据同步功能'
      );
    }
  }

  async testSyncLists() {
    const startTime = Date.now();
    try {
      const testList = this.testUtils.generateTestList();
      const response = await this.httpClient.post(config.apiEndpoints.sync.lists, {
        lists: [testList]
      });
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '同步清单',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试清单同步功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '同步清单',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试清单同步功能'
      );
    }
  }

  async testSyncTasks() {
    const startTime = Date.now();
    try {
      const testTask = this.testUtils.generateTestTask();
      const response = await this.httpClient.post(config.apiEndpoints.sync.tasks, {
        tasks: [testTask]
      });
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '同步任务',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试任务同步功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '同步任务',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试任务同步功能'
      );
    }
  }

  async testSyncConflictResolution() {
    const startTime = Date.now();
    try {
      const conflictData = {
        type: 'task',
        localVersion: {
          id: 'test_task_1',
          title: '本地版本',
          updatedAt: new Date().toISOString()
        },
        serverVersion: {
          id: 'test_task_1',
          title: '服务器版本',
          updatedAt: new Date(Date.now() + 1000).toISOString()
        },
        resolution: 'server'
      };
      
      const response = await this.httpClient.post(config.apiEndpoints.sync.resolveConflict, conflictData);
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '同步冲突解决',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试同步冲突解决功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '同步冲突解决',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试同步冲突解决功能'
      );
    }
  }

  async testSyncHistory() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get(config.apiEndpoints.sync.history + '?limit=10');
      
      this.testUtils.validateApiResponse(response, ['history']);
      
      if (!Array.isArray(response.data.history)) {
        throw new Error('同步历史格式无效');
      }
      
      this.testUtils.recordTestResult(
        '同步历史',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        `测试同步历史功能，获取到${response.data.history.length}条记录`
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '同步历史',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试同步历史功能'
      );
    }
  }

  async testAutoSyncSettings() {
    const startTime = Date.now();
    try {
      const autoSyncSettings = {
        enabled: true,
        interval: 300000, // 5分钟
        onlyOnWifi: false
      };
      
      const response = await this.httpClient.put(config.apiEndpoints.sync.autoSyncSettings, autoSyncSettings);
      
      this.testUtils.validateApiResponse(response);
      
      this.testUtils.recordTestResult(
        '自动同步设置',
        'DATA_SYNC',
        'passed',
        Date.now() - startTime,
        null,
        '测试自动同步设置功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '自动同步设置',
        'DATA_SYNC',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试自动同步设置功能'
      );
    }
  }

  /**
   * 测试数据管理功能
   */
  async testDataManagement() {
    console.log(chalk.cyan('\n💾 测试数据管理功能...'));
    
    await this.testDataExport();
    await this.testDataImport();
  }

  async testDataExport() {
    const startTime = Date.now();
    try {
      // 假设有数据导出API
      const response = await this.httpClient.get('/api/data/export');
      
      if (response.status === 200) {
        this.testUtils.recordTestResult(
          '数据导出',
          'DATA_MANAGEMENT',
          'passed',
          Date.now() - startTime,
          null,
          '测试数据导出功能'
        );
      } else {
        throw new Error('数据导出失败');
      }
    } catch (error) {
      this.testUtils.recordTestResult(
        '数据导出',
        'DATA_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试数据导出功能'
      );
    }
  }

  async testDataImport() {
    const startTime = Date.now();
    try {
      // 模拟数据导入
      const importData = {
        lists: [],
        tasks: []
      };
      
      const response = await this.httpClient.post('/api/data/import', importData);
      
      if (response.status === 200) {
        this.testUtils.recordTestResult(
          '数据导入',
          'DATA_MANAGEMENT',
          'passed',
          Date.now() - startTime,
          null,
          '测试数据导入功能'
        );
      } else {
        throw new Error('数据导入失败');
      }
    } catch (error) {
      this.testUtils.recordTestResult(
        '数据导入',
        'DATA_MANAGEMENT',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试数据导入功能'
      );
    }
  }

  /**
   * 测试统计功能
   */
  async testStatistics() {
    console.log(chalk.cyan('\n📊 测试统计分析功能...'));
    
    await this.testTaskStatistics();
    await this.testTimeUsageAnalysis();
    await this.testEfficiencyCharts();
    await this.testPomodoroStatistics();
    await this.testReportGeneration();
    await this.testProductivityTrend();
    await this.testCustomStatistics();
  }

  async testTaskStatistics() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get('/api/statistics/tasks?period=week');
      
      this.testUtils.validateApiResponse(response, ['statistics']);
      
      this.testUtils.recordTestResult(
        '任务完成统计',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '任务统计获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '任务完成统计',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试任务统计功能'
      );
    }
  }

  async testTimeUsageAnalysis() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get('/api/statistics/time-usage?period=month');
      
      this.testUtils.validateApiResponse(response, ['timeUsage']);
      
      this.testUtils.recordTestResult(
        '时间使用分析',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '时间统计获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '时间使用分析',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试时间使用分析功能'
      );
    }
  }

  async testEfficiencyCharts() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get('/api/statistics/efficiency?period=week&type=daily');
      
      this.testUtils.validateApiResponse(response, ['chartData']);
      
      this.testUtils.recordTestResult(
        '效率图表数据',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '效率数据获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '效率图表数据',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试效率图表功能'
      );
    }
  }

  async testPomodoroStatistics() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get('/api/statistics/pomodoro?period=week');
      
      this.testUtils.validateApiResponse(response, ['pomodoroStats']);
      
      this.testUtils.recordTestResult(
        '番茄钟统计',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '番茄钟统计获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '番茄钟统计',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试番茄钟统计功能'
      );
    }
  }

  async testReportGeneration() {
    const startTime = Date.now();
    try {
      // 测试日报生成
      const dailyResponse = await this.httpClient.get(`/api/statistics/daily-report?date=${new Date().toISOString().split('T')[0]}`);
      this.testUtils.validateApiResponse(dailyResponse, ['report']);
      
      // 测试周报生成
      const weeklyResponse = await this.httpClient.get('/api/statistics/weekly-report');
      this.testUtils.validateApiResponse(weeklyResponse, ['report']);
      
      // 测试月报生成
      const monthlyResponse = await this.httpClient.get('/api/statistics/monthly-report');
      this.testUtils.validateApiResponse(monthlyResponse, ['report']);
      
      this.testUtils.recordTestResult(
        '报告生成',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '日报、周报、月报生成成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '报告生成',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试报告生成功能'
      );
    }
  }

  async testProductivityTrend() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get('/api/statistics/productivity-trend?period=month');
      
      this.testUtils.validateApiResponse(response, ['trendData']);
      
      this.testUtils.recordTestResult(
        '生产力趋势分析',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '趋势分析获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '生产力趋势分析',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试生产力趋势分析功能'
      );
    }
  }

  async testCustomStatistics() {
    const startTime = Date.now();
    try {
      const customQueryData = {
        metrics: ['completionRate', 'averageTime', 'priorityDistribution'],
        filters: {
          dateRange: {
            start: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString(),
            end: new Date().toISOString()
          },
          priority: ['high', 'medium']
        },
        groupBy: 'day'
      };
      
      const response = await this.httpClient.post('/api/statistics/custom-query', customQueryData);
      
      this.testUtils.validateApiResponse(response, ['results']);
      
      this.testUtils.recordTestResult(
        '自定义统计查询',
        'STATISTICS',
        'passed',
        Date.now() - startTime,
        null,
        '自定义查询成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '自定义统计查询',
        'STATISTICS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试自定义统计查询功能'
      );
    }
  }

  /**
   * 测试应用设置功能
   */
  async testSettings() {
    console.log(chalk.cyan('\n⚙️ 测试应用设置功能...'));
    
    await this.testApplicationSettings();
  }

  async testApplicationSettings() {
    await this.testGetSettings();
    await this.testUpdateSettings();
    await this.testThemeSettings();
    await this.testNotificationSettings();
    await this.testLanguageSettings();
    await this.testBackupSettings();
    await this.testPrivacySettings();
  }

  async testGetSettings() {
    const startTime = Date.now();
    try {
      const response = await this.httpClient.get('/api/settings');
      
      this.testUtils.validateApiResponse(response, ['settings']);
      
      this.testUtils.recordTestResult(
        '获取应用设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '应用设置获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '获取应用设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试获取应用设置功能'
      );
    }
  }

  async testUpdateSettings() {
    const startTime = Date.now();
    try {
      const settingsData = {
        theme: 'dark',
        language: 'zh-CN',
        notifications: {
          enabled: true,
          sound: true,
          desktop: true
        },
        autoSave: true,
        defaultView: 'today'
      };
      
      const response = await this.httpClient.put('/api/settings', settingsData);
      
      this.testUtils.validateApiResponse(response, ['settings']);
      
      this.testUtils.recordTestResult(
        '更新应用设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '应用设置更新成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '更新应用设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试更新应用设置功能'
      );
    }
  }

  async testThemeSettings() {
    const startTime = Date.now();
    try {
      const themes = ['light', 'dark', 'auto'];
      
      for (const theme of themes) {
        const response = await this.httpClient.put('/api/settings/theme', { theme });
        this.testUtils.validateApiResponse(response, ['theme']);
      }
      
      this.testUtils.recordTestResult(
        '主题设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '主题设置测试成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '主题设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试主题设置功能'
      );
    }
  }

  async testNotificationSettings() {
    const startTime = Date.now();
    try {
      const notificationData = {
        enabled: true,
        sound: true,
        desktop: true,
        email: false,
        reminderTime: 15
      };
      
      const response = await this.httpClient.put('/api/settings/notifications', notificationData);
      
      this.testUtils.validateApiResponse(response, ['notifications']);
      
      this.testUtils.recordTestResult(
        '通知设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '通知设置更新成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '通知设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试通知设置功能'
      );
    }
  }

  async testLanguageSettings() {
    const startTime = Date.now();
    try {
      const languages = ['zh-CN', 'en-US', 'ja-JP'];
      
      for (const language of languages) {
        const response = await this.httpClient.put('/api/settings/language', { language });
        this.testUtils.validateApiResponse(response, ['language']);
      }
      
      this.testUtils.recordTestResult(
        '语言设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '语言设置测试成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '语言设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试语言设置功能'
      );
    }
  }

  async testBackupSettings() {
    const startTime = Date.now();
    try {
      const backupData = {
        autoBackup: true,
        backupInterval: 'daily',
        maxBackups: 30,
        includeSettings: true
      };
      
      const response = await this.httpClient.put('/api/settings/backup', backupData);
      
      this.testUtils.validateApiResponse(response, ['backup']);
      
      this.testUtils.recordTestResult(
        '备份设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '备份设置更新成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '备份设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试备份设置功能'
      );
    }
  }

  async testPrivacySettings() {
    const startTime = Date.now();
    try {
      const privacyData = {
        dataCollection: false,
        analytics: false,
        crashReports: true,
        shareUsageData: false
      };
      
      const response = await this.httpClient.put('/api/settings/privacy', privacyData);
      
      this.testUtils.validateApiResponse(response, ['privacy']);
      
      this.testUtils.recordTestResult(
        '隐私设置',
        'SETTINGS',
        'passed',
        Date.now() - startTime,
        null,
        '隐私设置更新成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '隐私设置',
        'SETTINGS',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试隐私设置功能'
      );
    }
  }

  /**
   * 测试AI功能
   */
  async testAiFeatures() {
    console.log(chalk.cyan('\n🤖 测试AI智能功能...'));
    
    await this.testAiTaskPlanning();
    await this.testAiTaskGeneration();
    await this.testAiSuggestions();
    await this.testAiTimeEstimation();
    await this.testAiTaskOptimization();
    await this.testAiAnalysisReport();
  }

  async testAiTaskPlanning() {
    const startTime = Date.now();
    try {
      // 测试AI任务规划API
      const planningData = {
        prompt: '帮我制定一个学习计划',
        timeframe: '1month',
        context: {
          category: 'learning',
          priority: 'medium'
        }
      };
      
      const response = await this.httpClient.post('/api/ai/planning', planningData);
      
      this.testUtils.validateApiResponse(response, ['plan']);
      
      this.testUtils.recordTestResult(
        'AI任务规划',
        'AI_FEATURES',
        'passed',
        Date.now() - startTime,
        null,
        '测试AI任务规划功能'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        'AI任务规划',
        'AI_FEATURES',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试AI任务规划功能'
      );
    }
  }

  async testAiTaskGeneration() {
    const startTime = Date.now();
    try {
      const aiTaskData = {
        prompt: '帮我制定一个学习计划',
        context: {
          timeframe: 'week',
          category: 'learning',
          priority: 'medium'
        }
      };
      
      const response = await this.httpClient.post('/api/ai/generate-tasks', aiTaskData);
      
      this.testUtils.validateApiResponse(response, ['tasks']);
      
      if (!Array.isArray(response.data.tasks)) {
        throw new Error('AI生成的任务格式无效');
      }
      
      this.testUtils.recordTestResult(
        'AI任务生成',
        'AI_FEATURES',
        'passed',
        Date.now() - startTime,
        null,
        `生成了${response.data.tasks.length}个任务`
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        'AI任务生成',
        'AI_FEATURES',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试AI任务生成功能'
      );
    }
  }

  async testAiSuggestions() {
    const startTime = Date.now();
    try {
      const suggestionData = {
        taskId: 'sample_task_1',
        type: 'optimization'
      };
      
      const response = await this.httpClient.post('/api/ai/suggestions', suggestionData);
      
      this.testUtils.validateApiResponse(response, ['suggestions']);
      
      this.testUtils.recordTestResult(
        '智能建议',
        'AI_FEATURES',
        'passed',
        Date.now() - startTime,
        null,
        '智能建议获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '智能建议',
        'AI_FEATURES',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试智能建议功能'
      );
    }
  }

  async testAiTimeEstimation() {
    const startTime = Date.now();
    try {
      const estimateData = {
        title: '完成项目报告',
        description: '整理本月项目进度，撰写详细的项目报告文档',
        complexity: 'medium'
      };
      
      const response = await this.httpClient.post('/api/ai/estimate-time', estimateData);
      
      this.testUtils.validateApiResponse(response, ['estimatedTime']);
      
      this.testUtils.recordTestResult(
        'AI时间估算',
        'AI_FEATURES',
        'passed',
        Date.now() - startTime,
        null,
        `估算时间：${response.data.estimatedTime}分钟`
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        'AI时间估算',
        'AI_FEATURES',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试AI时间估算功能'
      );
    }
  }

  async testAiTaskOptimization() {
    const startTime = Date.now();
    try {
      const optimizeData = {
        tasks: [
          {
            id: 'task1',
            title: '学习Vue.js',
            priority: 'medium',
            estimatedTime: 120
          },
          {
            id: 'task2',
            title: '完成项目报告',
            priority: 'high',
            estimatedTime: 90
          }
        ]
      };
      
      const response = await this.httpClient.post('/api/ai/optimize-tasks', optimizeData);
      
      this.testUtils.validateApiResponse(response, ['optimizedTasks']);
      
      this.testUtils.recordTestResult(
        '任务优化',
        'AI_FEATURES',
        'passed',
        Date.now() - startTime,
        null,
        '任务优化建议获取成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        '任务优化',
        'AI_FEATURES',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试任务优化功能'
      );
    }
  }

  async testAiAnalysisReport() {
    const startTime = Date.now();
    try {
      const analysisData = {
        period: 'week',
        includeProductivity: true,
        includePatterns: true
      };
      
      const response = await this.httpClient.post('/api/ai/analyze', analysisData);
      
      this.testUtils.validateApiResponse(response, ['analysis']);
      
      this.testUtils.recordTestResult(
        'AI分析报告',
        'AI_FEATURES',
        'passed',
        Date.now() - startTime,
        null,
        'AI分析报告生成成功'
      );
    } catch (error) {
      this.testUtils.recordTestResult(
        'AI分析报告',
        'AI_FEATURES',
        'failed',
        Date.now() - startTime,
        error.message,
        '测试AI分析报告功能'
      );
    }
  }
}

// 如果直接运行此文件，则执行测试
if (require.main === module) {
  const runner = new ApiTestRunner();
  runner.runAllTests().catch(console.error);
}

module.exports = ApiTestRunner;